package guda.house.biz.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import guda.house.biz.bo.WaterFeeBO;
import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.form.bill.BillWaterEditForm;
import guda.house.biz.form.bill.BillWaterForm;
import guda.house.dal.dataobject.BillElecDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.house.biz.BillWaterBiz;
import guda.house.dal.dao.BillWaterDAO;
import guda.house.dal.dataobject.BillWaterDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class BillWaterBizImpl implements BillWaterBiz{

    private final static Logger logger = LoggerFactory.getLogger(BillWaterBizImpl.class);

    @Autowired
    private BillWaterDAO billWaterDAO;
    @Autowired
    private WaterFeeBO waterFeeBO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            BillWaterDO billWaterDO = billWaterDAO.getBillWaterById(id);
            bizResult.data.put("billWaterDO", billWaterDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query BillWater error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = billWaterDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<BillWaterDO> billWaterList = billWaterDAO.selectForPage(baseQuery);
            bizResult.data.put("billWaterList", billWaterList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view BillWater list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            billWaterDAO.delBillWaterById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete billWater error", e);
        }
        return bizResult;
    }

    public BizResult create(BillWaterForm billWaterForm) {
        BizResult bizResult = new BizResult();
        try {
            BillWaterDO billWaterDO = billWaterForm.toDO();
            billWaterDO.setRecordTotal(billWaterForm.getRecordCurrent() - billWaterForm.getRecordLast());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(billWaterDO.getGmtStart());
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH)+1;

            //判断是否已经录入该月份的账单
            List<BillWaterDO> bill = billWaterDAO.getBill(billWaterForm.getSuiteId(), year, month);
            if(bill.size()>0){
                bizResult.code = CommonResultCode.BILL_WATER_REPEAT;
                return bizResult;
            }

            billWaterDO.setRecordYear(year);
            billWaterDO.setRecordMonth(month);
            billWaterDO.setGmtRecord(new Date());
            billWaterDO.setIsPayed(0);

            long payTotal = waterFeeBO.countWaterFee(billWaterForm.getVillageId(),billWaterForm.getSuiteId(), billWaterDO.getRecordTotal());
            billWaterDO.setPayTotal(payTotal);

            long id = billWaterDAO.insert(billWaterDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create BillWater error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(BillWaterEditForm billWaterEditForm) {
        BizResult bizResult = new BizResult();
        try {

            BillWaterDO billWaterDO = billWaterEditForm.toDO();
            billWaterDO.setRecordTotal(billWaterEditForm.getRecordCurrent() - billWaterEditForm.getRecordLast());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(billWaterDO.getGmtStart());
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH)+1;

            //判断是否已经录入该月份的账单
            List<BillWaterDO> bill = billWaterDAO.getBill(billWaterEditForm.getSuiteId(), year, month);
            if(bill.size()>1){
                bizResult.code = CommonResultCode.BILL_WATER_REPEAT;
                return bizResult;
            }else if(bill.size() == 1){
                if(bill.get(0).getBillId().intValue()!= billWaterEditForm.getBillId().intValue()){
                    bizResult.code = CommonResultCode.BILL_WATER_REPEAT;
                    return bizResult;
                }
            }


            billWaterDO.setRecordYear(year);
            billWaterDO.setRecordMonth(month);
            billWaterDO.setGmtRecord(new Date());
            billWaterDO.setIsPayed(0);

            long payTotal = waterFeeBO.countWaterFee(billWaterEditForm.getVillageId(),billWaterEditForm.getSuiteId(), billWaterDO.getRecordTotal());
            billWaterDO.setPayTotal(payTotal);

            billWaterDAO.updateByIdSelective(billWaterDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update BillWater error", e);
        }
        return bizResult;
    }



}
